﻿namespace QuickMail.View.Controls
{
    using System.Globalization;
    using System.Windows;

    using QuickMail.BusinessLogic;
    using QuickMail.DataModels;
    using QuickMail.Properties;

    /// <summary>
    /// Extended settings control to connect to the mail.
    /// </summary>
    public partial class ExtendedConnectSettings
    {
        private readonly TestConnectionController _tcController;

        public ExtendedConnectSettings(TestConnectionController tcController)
        {
            InitializeComponent();
            SetCurrentTranslate();

            _tcController = tcController;
        }

        /// <summary>
        /// Sets the current translation for form elements.
        /// </summary>
        private void SetCurrentTranslate()
        {
            //FullNameTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.FullName;
            //FullNameTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.FullNameTt;
            GetServerTypeTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.GetServerType;
            GetServerTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Server;
            GetServerTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.GetServerTt;
            GetPortTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Port;
            GetPortTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.GetPortTt;
            GetSafetyConnectionCheckBox.Content = StaticFields.Lang.ConnectSettingsWindow.SafetyConnection;
            GetSafetyConnectionCheckBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.SafetyConnectionTt;
            GetEmailTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Email;
            GetEmailTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.EmailTt;
            GetPasswordTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Password;
            GetPasswordBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.PasswordTt;
            GetStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Status;

            SendServerTypeTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.SendServerType;
            SendServerTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Server;
            SendServerTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.SendServerTt;
            SendPortTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Port;
            SendPortTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.SendPortTt;
            SendCryptTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Encryption;
            SendAuthenticationTypeTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Authentication;
            SendEmailTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Email;
            SendEmailTextBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.EmailTt;
            SendPasswordTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Password;
            SendPasswordBox.ToolTip = StaticFields.Lang.ConnectSettingsWindow.PasswordTt;
            SendStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.Status;

            TestConnectionButton.Content = StaticFields.Lang.ConnectSettingsWindow.TestConnection;
        }

        /// <summary>
        /// Loading settings in fields of form
        /// </summary>
        public void LoadSettings()
        {
            //FullNameTextBox.Text = Settings.Default.FullName;
            GetServerTypeComboBox.SelectedIndex = Settings.Default.GetServerType;
            GetServerTextBox.Text = Settings.Default.GetServer;
            GetPortTextBox.Text = Settings.Default.GetPort.ToString(CultureInfo.InvariantCulture);
            GetSafetyConnectionCheckBox.IsChecked = Settings.Default.GetUseSsl;
            GetEmailTextBox.Text = Settings.Default.GetEmail;
            GetPasswordBox.Password = CryptWorker.Decrypt(Settings.Default.GetPassword);

            SendServerTextBox.Text = Settings.Default.SendServer;
            SendPortTextBox.Text = Settings.Default.SendPort.ToString(CultureInfo.InvariantCulture);
            SendCryptTypeComboBox.SelectedIndex = Settings.Default.SendCryptType;
            SendAutinfecationTypeComboBox.SelectedIndex = Settings.Default.SendAutinfecationType;
            SendEmailTextBox.Text = Settings.Default.SendEmail;
            SendPasswordBox.Password = CryptWorker.Decrypt(Settings.Default.SendPassword);
        }

        /// <summary>
        /// Save settings to file
        /// </summary>
        public void SaveSettings()
        {
            Settings.Default.GetServer = GetServerTextBox.Text.Trim();
            Settings.Default.GetServerType = GetServerTypeComboBox.SelectedIndex;
            Settings.Default.GetPort = ushort.Parse(GetPortTextBox.Text.Trim());
            Settings.Default.GetUseSsl = GetSafetyConnectionCheckBox.IsChecked == true;
            Settings.Default.GetEmail = GetEmailTextBox.Text.Trim();
            Settings.Default.GetPassword = CryptWorker.Encrypt(GetPasswordBox.Password);
            
            Settings.Default.SendServer = SendServerTextBox.Text.Trim();
            Settings.Default.SendPort = ushort.Parse(SendPortTextBox.Text.Trim());
            Settings.Default.SendCryptType = SendCryptTypeComboBox.SelectedIndex;
            Settings.Default.SendAutinfecationType = SendAutinfecationTypeComboBox.SelectedIndex;
            Settings.Default.SendEmail = SendEmailTextBox.Text.Trim();
            Settings.Default.SendPassword = CryptWorker.Encrypt(SendPasswordBox.Password);
            Settings.Default.Save();
        }

        /// <summary>
        /// Checks the correctness of input fields.
        /// </summary>
        /// <returns>true - correct, false - incorrect</returns>
        public bool CheckingFields()
        {
            bool result = true;

            //if (string.IsNullOrEmpty(FullNameTextBox.Text.Trim()))
            //{
            //    FullNameTextBox.Background = StaticFields.ErrorColorBrush;
            //    FullNameTextBox.Focus();
            //    result = false;
            //}

            if (string.IsNullOrEmpty(GetServerTextBox.Text.Trim()))
            {
                GetServerTextBox.Background = StaticFields.ErrorColorBrush;
                result = false;
            }

            ushort tempGetPort;
            if (!ushort.TryParse(GetPortTextBox.Text.Trim(), out tempGetPort))
            {
                GetPortTextBox.Background = StaticFields.ErrorColorBrush;
                result = false;
            }

            if (string.IsNullOrEmpty(GetEmailTextBox.Text.Trim()))
            {
                GetEmailTextBox.Background = StaticFields.ErrorColorBrush;
                result = false;
            }


            if (string.IsNullOrEmpty(SendServerTextBox.Text.Trim()))
            {
                SendServerTextBox.Background = StaticFields.ErrorColorBrush;
                result = false;
            }

            ushort tempSetPort;
            if (!ushort.TryParse(SendPortTextBox.Text.Trim(), out tempSetPort))
            {
                SendPortTextBox.Background = StaticFields.ErrorColorBrush;
                SendPortTextBox.Focus();
                result = false;
            }

            if (string.IsNullOrEmpty(SendEmailTextBox.Text.Trim()))
            {
                SendEmailTextBox.Background = StaticFields.ErrorColorBrush;
                SendEmailTextBox.Focus();
                result = false;
            }

            return result;
        }

        /// <summary>
        /// Shows the result of the incoming connection attempt.
        /// </summary>
        public void ShowIncomingStatus(bool status)
        {
            if (status)
            {
                GetConnectionStatusTextBlock.Foreground = StaticFields.SuccessTextColorBrush;
                GetConnectionStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.SuccessConnection;
            }
            else
            {
                GetConnectionStatusTextBlock.Foreground = StaticFields.ErrorTextColorBrush;
                GetConnectionStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.ErrorConnection;
            }
        }

        /// <summary>
        /// Shows the result of the outgoing connection attempt.
        /// </summary>
        public void ShowOutgoingStatus(bool status)
        {
            if (status)
            {
                SendConnectionStatusTextBlock.Foreground = StaticFields.SuccessTextColorBrush;
                SendConnectionStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.SuccessConnection;
            }
            else
            {
                SendConnectionStatusTextBlock.Foreground = StaticFields.ErrorTextColorBrush;
                SendConnectionStatusTextBlock.Text = StaticFields.Lang.ConnectSettingsWindow.ErrorConnection;
            }
        }

        /// <summary>
        /// Shows the wait status.
        /// </summary>
        private void ShowWaitStatus()
        {
            GetConnectionStatusTextBlock.Foreground =
                SendConnectionStatusTextBlock.Foreground = 
                StaticFields.NormalTextColorBrush;

            GetConnectionStatusTextBlock.Text =
                SendConnectionStatusTextBlock.Text =
                StaticFields.Lang.ConnectSettingsWindow.WaitConnection;

            TestConnectionButton.IsEnabled = false;
        }

        /// <summary>
        /// Test connection
        /// </summary>
        private void TestConnect_OnClick(object sender, RoutedEventArgs e)
        {
            if(!CheckingFields())
                return;

            SaveSettings();
            ShowWaitStatus();

            _tcController.TestConnectionComplete += OnTestConnectionComplete;
            _tcController.RunTestConntection();
        }

        /// <summary>
        /// Show result after tests connection
        /// </summary>
        private void OnTestConnectionComplete(bool incomingResult, bool outgoingResult)
        {
            ShowIncomingStatus(incomingResult);
            ShowOutgoingStatus(outgoingResult);
            TestConnectionButton.IsEnabled = true;
        }
    }
}
